
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="90px" class="demo-ruleForm">
          <el-form-item label="线路名称:" prop="line_name">
            <el-input v-model="ruleForm.line_name" placeholder="请输入线路名称" />
          </el-form-item>
          <el-form-item label="线路代码:" prop="line_code">
            <el-input v-model="ruleForm.line_code" placeholder="请输入线路代码" />
          </el-form-item>

          <el-form-item label="起运国:" prop="start_country">
            <el-select
              v-model="ruleForm.start_country"
              style="width:100%"
              placeholder="请选择起运国"
              clearable
            >
              <el-option
                v-for="(item,index) in countryCodeList"
                :key="index"
                :label="item.country_name"
                :value="item.country_name"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="目的国:" prop="end_country">
            <el-select
              v-model="ruleForm.end_country"
              style="width:100%"
              placeholder="请选择目的国"
              clearable
            >
              <el-option
                v-for="(item,index) in countryCodeList"
                :key="index"
                :label="item.country_name"
                :value="item.country_name"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createLine, updateLine } from '@/api/base/lines'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    },
    countryCodeList: {
      type: Array,
      default: () => {

      } }
  },
  data() {
    return {
      showDialog: false,
      ruleForm: {
        'line_name': '',

        'line_code': '',

        'start_country': '',

        'end_country': '',

        'start_country_code': '',

        'end_country_code': ''
      },
      rules: {
        line_name: [
          { required: true, message: '请输入线路名称', trigger: 'blur' }
        ],
        line_code: [
          { required: true, message: '请输入线路代码' }
        ],
        start_country: [
          { required: true, message: '请选择起运国' }
        ],
        end_country: [
          { required: true, message: '请选择目的国' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      console.log('==', this.detial, this.visible, this.status, this.showDialog)
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          this.ruleForm = this.detial
        } else {
          this.resetForm()
        }
      }
    }
  },
  methods: {
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.ruleForm = {
        'line_name': '',
        'line_code': '',
        'start_country': '',
        'end_country': '',
        'start_country_code': '',
        'end_country_code': ''
      }
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.countryCodeList.forEach(item => {
            if (item.country_name === this.ruleForm.start_country) {
              this.ruleForm.start_country_code = item.country_code
            }
            if (item.country_name === this.ruleForm.end_country) {
              this.ruleForm.end_country_code = item.country_code
            }
          })
          createLine(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.countryCodeList.forEach(item => {
            if (item.country_name === this.ruleForm.start_country) {
              this.ruleForm.start_country_code = item.country_code
            }
            if (item.country_name === this.ruleForm.end_country) {
              this.ruleForm.end_country_code = item.country_code
            }
          })
          updateLine(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
